package com.wy.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wy.common.R;

import com.wy.service.IMessageService;
import com.wy.entity.Message;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 王昭贵
 * @since 2023-05-15
 */
@RestController
@RequestMapping("/message")
public class MessageController {

    @Resource
    private IMessageService messageService;

    /****
     * 删除留言
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public R delete(@PathVariable Integer id) {
        messageService.removeById(id);
        return R.success("");
    }

    /****
     * 新增 修改
     * @param message
     * @return
     */
    @PostMapping
    public R save(@RequestBody Message message) {
        messageService.saveOrUpdate(message);
        return R.success("");
    }

    /****
     * 获取留言列表分页
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R findPage(@RequestParam Integer page,
                      @RequestParam Integer pageSize,@RequestParam String name) {
        LambdaQueryWrapper<Message> queryWrapper = new LambdaQueryWrapper<>();
        if(StrUtil.isNotBlank(name)){
            queryWrapper.like(Message::getName,name);
        }
        queryWrapper.orderByDesc(Message::getCreateTime);
        return R.success(messageService.page(new Page<>(page, pageSize), queryWrapper));
    }
}

